#include "MonitorData.h"
#include "ui_MonitorData.h"
#include "RealtimeData.h"
#include "HistoricalData.h"

MonitorData::MonitorData(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MonitorData)
{
    ui->setupUi(this);

    this->buttonInit();

}

MonitorData::~MonitorData()
{
    delete ui;
}

void MonitorData::buttonInit()
{
    this->checkedStyle = R"(
        QPushButton {
            background-color: #FFFFFF;
            border-radius: 15px;
            color: #333333;
            padding: 0 10px;
            border: none;
            font-size:15px
        }
        QPushButton:hover {

            background-color: #FFFFFF;
        }
        QPushButton:pressed {
            background-color: #FFFFFF;
        }
        QPushButton:disabled {
            background-color: #FFFFFF;
            border-color: #F0F0F0;
            color: #AAAAAA;
        }
    )";

    this->noCheckedStyle = R"(
        QPushButton {
            background-color: #E0E0E0;
            border: none;
            border-radius: 15px;
            color: #333333;
            padding: 0 10px;
            font-size:15px
        }
        QPushButton:hover {
            background-color: #D0D0D0;
        }
        QPushButton:pressed {
            background-color: #C0C0C0;
        }
        QPushButton:disabled {
            background-color: #F0F0F0;
            color: #999999;
        }
    )";

    QList<QPushButton*> button_list = {
        this->ui->pushButton_readTime,
        this->ui->pushButton_history
    };

    QList<QWidget*> page_list = {
        new RealtimeData(),
        new HistoricalData(),
    };

    int index = 0;
    foreach(QPushButton* button, button_list)
    {
        this->ui->stackedWidget->insertWidget(index, page_list[index]);
        connect(button, &QPushButton::clicked, this, [=](){
            QPushButton* buttonSender = qobject_cast<QPushButton*>(sender());
            if(buttonSender)
            {
                foreach(QPushButton* btn, button_list)
                {
                    if(btn == buttonSender)
                    {
                        btn->setStyleSheet(this->checkedStyle);
                    }
                    else
                    {
                        btn->setStyleSheet(this->noCheckedStyle);
                    }
                }
            }
            this->ui->stackedWidget->setCurrentIndex(index);
        });
        index++;
    }

    this->ui->stackedWidget->setCurrentIndex(0);
}

